package com.woniu.presell.service.impl;


import com.woniu.presell.entity.Check;
import com.woniu.presell.entity.Higher;
import com.woniu.presell.entity.Licence;
import com.woniu.presell.entity.Proposer;
import com.woniu.presell.mapper.HigherMapper;
import com.woniu.presell.mapper.LicenceMapper;
import com.woniu.presell.service.IProposerService;
import com.woniu.presell.service.LicenceService;
import io.seata.spring.annotation.GlobalTransactional;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;

/**
 * @program: house-after
 * @description:
 * @author: qqq
 * @create: 2022-12-13 12:03
 **/
@Service
public class LicenceImpl implements LicenceService {
    @Autowired
    private LicenceMapper licenceMapper;
    @Autowired
    private IProposerService proposerService;
    @Autowired
    private HigherMapper higherMapper;
    @Override
    public int insertSelective(Licence record) {
        return licenceMapper.insertSelective(record);
    }

    /**
     * @Description
     * 前端先查出来申请人信息进行补充，对申请人做修改
     * 自己填写楼盘信息，新增楼盘
     * 然后补充预售许可证表信息文件，对预售许可证做修改
     */
    @Override
    public int updateByPrimaryKeySelective(Licence record,Integer id) {
        record.setLicenceStaus("<已入库>");
        record.setHigherId(id);
        return licenceMapper.updateByPrimaryKeySelective(record);
    }

    @Override
    public int updateByid(Integer id) {
        Licence licence =new Licence();
        licence.setLicenceId(id);
        licence.setLicenceStaus("<已归档>");
        return licenceMapper.updateByPrimaryKeySelective(licence);
    }


    /**
     * @Description
     * 商品房预售许可证登记时，
     * 先做申请人和审核表的添加，
     * 再做预售许可证表的添加并关联以上
     */
    @Override
    @GlobalTransactional
    public int insertPreAndProAndCheck(Licence licence, Proposer proposer, Check check) {
        int pid = proposerService.proposerAdd(proposer);
        int cid = proposerService.checkAdd(check);
        licence.setLicenceNo(System.currentTimeMillis()+"");
        licence.setLicenceStaus("<已受理>");
        licence.setLicenceTime(new Date());
        licence.setBak1("商品预售许可证登记");
        licence.setProposerId(pid);
        licence.setCheckId(cid);
        return licenceMapper.insertSelective(licence);
    }

    @Override
    public Licence selectLid(Long id) {
        return licenceMapper.selectLid(id);
    }

    @Override
    public int updateByHigher(Integer id) {
        Higher higher = higherMapper.selectByhid(id);
        higher.setHigherStatus("五证齐全");
        return higherMapper.updateByPrimaryKeySelective(higher);
    }


}
